import { ToastController } from 'ionic-angular/components/toast/toast-controller';
import { LoadingController } from 'ionic-angular/components/loading/loading-controller';
import { Component } from '@angular/core';
import { IonicPage, NavController, NavParams } from 'ionic-angular';
import { ViewController } from 'ionic-angular/navigation/view-controller';
import { BaseUI } from '../../common/baseui';
import { RestProvider } from '../../providers/rest/rest'
import { Storage } from '@ionic/storage'
import {RegisterPage} from '../register/register'

/**
 * Generated class for the LoginPage page.
 *
 * See https://ionicframework.com/docs/components/#navigation for more info on
 * Ionic pages and navigation.
 */

@IonicPage()
@Component({
  selector: 'page-login',
  templateUrl: 'login.html',
})
export class LoginPage  extends BaseUI{

  mobile: any
  password: any
  errorMessage: any
  constructor(public navCtrl: NavController, 
              public navParams: NavParams,
              public rest: RestProvider,
              public viewCtrl: ViewController,
              public toastCtrl: ToastController,
              public loadingCtrl: LoadingController,
              public storage: Storage) {
              super()  //调用父类的构造函数
  }

  login() {
    let loading = super.showLoading(this.loadingCtrl, "登录中")
    this.rest.login(this.mobile, this.password) 
    .subscribe(
      f=>{
        if(f["Status"]=="OK"){
          // 登录成功
          // 可以存储接口返回的token
          console.log(this.storage)
          this.storage.set('userId', f["UserId"])
          console.log(f["UserId"])
          loading.dismiss()
          this.dismiss()
        }else {
          loading.dismiss()
          super.showToast(this.toastCtrl,f["StatusContent"])
        }
      },
      error=>this.errorMessage = <any>error
    )
  }

  /**
   * 关闭当前页面的方法
   * 
   * @memberof LoginPage
   */
  dismiss() {
    this.viewCtrl.dismiss()  
  }

  pushRegister() {
    this.navCtrl.push(RegisterPage)
  }
}
